﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using RoundLineCode;

namespace Utils
{
    public static class CurvyLine
    {
        public static List<RoundLine> GetCurvyLine(Vector3 origin, Vector3 destination, float spazFactor, int seed)
        {
            var distance = Vector3.Distance(origin, destination);

            var linesBetweenPoints = Convert.ToInt16(distance * (int)(spazFactor * 100));

            var points = new List<Vector3> { origin };

            var random = new Random(seed);

            var spazness = 0f;
            for (var i = 0; i < linesBetweenPoints; i++)
            {
                origin = points[points.Count - 1];

                distance = Vector3.Distance(origin, destination);

                spazness -= random.Next(-3, 3) * spazFactor;

                if (i / linesBetweenPoints > .8 || i / linesBetweenPoints < .2)
                    spazness *= .4f;

                var step = (distance / (linesBetweenPoints - i)) - Math.Abs(spazness);

                Vector3 diff;
                Vector3.Subtract(ref destination, ref origin, out diff);
                Vector3 unitVector;
                Vector3.Normalize(ref diff, out unitVector);

                var newPoint = origin;
                newPoint.X += (unitVector.X * step) + spazness;
                newPoint.Y += (unitVector.Y * step) + spazness;

                points.Add(newPoint);
            }

            points.Add(destination);

            var roundLines = new List<RoundLine>();
            for (var i = 0; i < points.Count; i++)
            {
                if (i <= 0) continue;

                roundLines.Add(new RoundLine(points[i - 1].X, points[i - 1].Y, points[i].X, points[i].Y));
            }
            return roundLines;
        }
    }
}
